package com.jk.customer.service.impl;

import com.jk.customer.dao.SellerDao;
import com.jk.customer.entity.*;
import com.jk.customer.service.SellerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.UUID;

/**
 * 方法实现说明    (serviceImpl)
 * @author      潘天赐
 * @return
 * @exception
 * @date        2021/10/11 20:33
 */
@Service
public class SellerServiceImpl implements SellerService {

    @Autowired
    private SellerDao sellerDao;

    @Override
    public List<MaterialsEntity> queryMaterials(String showPriceId) {
        return sellerDao.queryMaterials(showPriceId);
    }

    @Override
    public void addOffer(OfferFormEntity offer, HttpSession session) {
        //UUID
        UUID uuid = UUID.randomUUID();
        String s = uuid.toString();
        String s1 = s.replaceAll("-", "");
        System.out.println(s1);
        offer.setOfferFormId(s1);
        //用户
        UserEntity user = (UserEntity) session.getAttribute("user");
        offer.setOfferName(user.getUserRealName());
        //修改状态询价状态
        //String queryPriceId = offer.getQueryPriceId();
        String queryPriceId = (String) session.getAttribute("showPriceId");
        sellerDao.updateQueryPriceStatus(queryPriceId);
        //获取id
        String productId = (String) session.getAttribute("productId");
        offer.setCommonId(productId);
        offer.setUserId(user.getUserId());
        sellerDao.addOffer(offer);
    }

    @Override
    public List<MaterialsEntity> queryMaterialsOffer(String id, HttpSession session) {
        UserEntity user = (UserEntity) session.getAttribute("user");
        return sellerDao.queryMaterialsOffer(id,user.getUserId());
    }

    @Override
    public OfferFormEntity findByOffer(Integer oid, HttpSession session) {
        UserEntity user = (UserEntity) session.getAttribute("user");
        return sellerDao.findByOffer(oid,user.getUserId());
    }

    @Override
    public void updateOffer(OfferFormEntity offer) {
        sellerDao.updateOffer(offer);
    }

    @Override
    public List<ProductEntity> queryMaterialsAll(ProductEntity product, HttpSession session) {
        UserEntity user = (UserEntity) session.getAttribute("user");
        return sellerDao.queryMaterialsAll(product,user.getUserId());
    }

    @Override
    public void updateMaterials(MaterialsEntity materials) {
        //获取主表id
        //String queryPriceId = materials.getQueryPriceId();
        //MaterialsEntity materialsEntity = sellerDao.findbyProductId(queryPriceId);
        //materials.setProductId(materialsEntity.getProductId());
        //计算总价
        /*Integer materialsCount = materials.getMaterialsCount();
        Double price = materials.getPrice();
        Double freight = materials.getFreight();
        Double assemblingCharge = materials.getAssemblingCharge();
        Double businessPrice = materials.getBusinessPrice();
        Double a = materialsCount*price+freight+assemblingCharge+businessPrice;
        System.out.println(a);
        materials.setTotalPrices(a);*/
        //修改
        sellerDao.updateMaterialsPTC(materials);
        //sellerDao.updateOfferPTC(materials);
        sellerDao.updateProductPTC(materials);

    }

    @Override
    public MaterialsEntity export(Integer id, HttpSession session) {
        UserEntity user = (UserEntity) session.getAttribute("user");
        return sellerDao.export(id,user.getUserId());
    }

    @Override
    public List<PictureFile> queryPictureFile(String id, HttpSession session) {
        UserEntity user = (UserEntity) session.getAttribute("user");
        return sellerDao.queryPictureFile(id,user.getUserId());
    }

    @Override
    public void addFile(PictureFile file, HttpSession session) {
        UserEntity user = (UserEntity) session.getAttribute("user");
        file.setUserId(user.getUserId());
        sellerDao.addFile(file);
    }

    @Override
    public void deleteFile(Integer id) {
        sellerDao.deleteFile(id);
    }

    @Override
    public List<MaterialsTypeEntity> queryMaterialsType() {
        return sellerDao.queryMaterialsType();
    }

    @Override
    public void addPictureMaterials(MaterialsEntity materials, HttpSession session) {
        UserEntity user = (UserEntity) session.getAttribute("user");
        materials.setMaterialsUserName(user.getUserRealName());
        //随机数 6
        int i = new Random().nextInt()*100000;
        materials.setMaterialsId(i);

        sellerDao.addPictureMaterials(materials);
        sellerDao.updateQueryPictureStatus(materials.getCommonQueryId());
    }

    @Override
    public MaterialsEntity queryPictureMaterials(String id) {
        return sellerDao.queryPictureMaterials(id);
    }

    @Override
    public void updatePictureMaterials(MaterialsEntity materials) {
        sellerDao.updatePictureMaterials(materials);
    }

    @Override
    public BusinessClass findByOrder(String id,HttpSession session) throws ParseException {
        BusinessClass byOrder = sellerDao.findByOrder(id);
        UserEntity user = (UserEntity) session.getAttribute("user");
        byOrder.setUserName(user.getUserRealName());
        byOrder.setUserRealNameTo(byOrder.getUserRealName());

        //计算 数据库日期-当前系统时间 剩余天数
        String offerFormValidTime = byOrder.getOfferFormValidTime();
        Date date = new Date();
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = sim.parse(offerFormValidTime);
        long time = date.getTime();
        long offerTime = parse.getTime();
        long a = offerTime-time;
        String aa = a/1000/60/60/24+1+"";
        byOrder.setOfferFormValidTime(aa);

        return byOrder;
    }

    @Override
    public List<MaterialsEntity> queryMaterialsMessage(String id) {
        return sellerDao.queryMaterialsMessage(id);
    }
}
